package com.sj.purchase.modules.stages.config.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sj.purchase.manager.stages.StagesOrderManager;
import com.sj.purchase.modules.stages.model.StagesOrder;
import com.sj.purchase.modules.stages.repository.StagesOrderRepository;
import com.sj.purchase.util.Date2StrUtils;
import com.sj.purchase.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author yangrd
 * @date 2018/8/10
 **/
@Component
public class StagesTask {

    private RestTemplate restTemplate;

    private StagesOrderRepository stagesOrderRepository;

    private StagesOrderManager stagesOrderManager;

    private String ecWMSApi;

    public StagesTask(RestTemplate restTemplate, StagesOrderRepository stagesOrderRepository, StagesOrderManager stagesOrderManager, @Value("${sj.ecwms}") String ecWMSApi) {
        this.restTemplate = restTemplate;
        this.stagesOrderRepository = stagesOrderRepository;
        this.stagesOrderManager = stagesOrderManager;
        this.ecWMSApi = ecWMSApi;
    }

    /**
     * 同步分期购的数据
     *
     * @param day yyyy/MM/dd
     */
    public void syncStagesOrder(String day) {
        Date start = Date2StrUtils.parse(day);
        Date end = DateUtils.addDays(start, 1);
        List<StagesOrder> stagesOrderList = stagesOrderManager.listAll(start, end).stream().map(this::map).collect(Collectors.toList());
        stagesOrderRepository.save(stagesOrderList);
    }


    /**
     * 同步出库数据
     *
     * @param day yyyy/MM/dd
     */
    public void syncWMSOutOrder(String day) {
        Date start = Date2StrUtils.parse(day);
        Date end = DateUtils.addDays(start, 1);
        String tomorrowStr = Date2StrUtils.str(end);
        String url = String.format("%s/shipping-order?start=%s&end=%s", ecWMSApi, day, tomorrowStr);
        String jsonStr = restTemplate.getForObject(url, String.class);
        JSONArray jsonArray = JSON.parseArray(jsonStr);
        List<StagesOrder> stagesOrderList = jsonArray.stream().map(o -> (JSONObject) o).map(this::map).collect(Collectors.toList());
        stagesOrderRepository.save(stagesOrderList);
    }

    private StagesOrder map(com.sj.purchase.manager.stages.StagesOrder stagesOrder) {
        StagesOrder stagesOrder1 = new StagesOrder();
        stagesOrder1.setOrderNo(stagesOrder.getId());
        stagesOrder1.setShopName(stagesOrder.getShop());
        BeanUtils.copyProperties(stagesOrder, stagesOrder1);
        return stagesOrder1;
    }

    private StagesOrder map(JSONObject jsonObject) {
        String orderNo = jsonObject.getString("orderNo");
        //销售价
        Double payment = jsonObject.getDouble("payment");
        //记录发货时间
        Date orderDate = jsonObject.getDate("deliveryTime");
        //店铺
        String shopName = jsonObject.getString("shopName");

        return Optional.ofNullable(stagesOrderRepository.findOne(orderNo)).orElseGet(StagesOrder::new).setOrderNo(orderNo).setPayment(payment).setOrderDate(orderDate).setShopName(shopName);
    }


}
